
<% include("../common/head.html"){} %>

<script>
$("#user_submit_search").click(function () {
                $('#dg').datagrid('load',{
					username: $('#username').val()
				});
            });
</script>

<div class="easyui-panel" data-options="fit:true,boarder:false">
	<table class="easyui-datagrid" id="dg" data-options="url:'/user/query',
		rownumbers:true,pagination:true,pageNumber:1,pageSize:20,pagePosition:'bottom',
		fit:true,toolbar:'#userSearchform',singleSelect:true,showFooter:true">
		<thead>
			<tr>
				<th data-options="field:'id',width:100">用户信息编号</th>
				<th data-options="field:'username',width:100">用户名称</th>
				<th data-options="field:'status'">用户状态</th>
				<th data-options="field:'create_time'">注册日期</th>
				<th data-options="field:'_operate',width:80,align:'center',formatter:formatOper">操作</th>  
			</tr>
		</thead>
	</table>
	
	<div id ="userSearchform" class="tableSearch">
		<input type="text" id="username" size=10  placeholder="用户名称" />
    	<button class="btn btn-default" id="user_submit_search">查询</button>
    	<button class="btn btn-default" onclick="resetForm();">重 置</button>
    	<br/>
		<% if(so.hasRole("admin")){%>
			<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="newUser()">新增用户 </a> 
		<%}%>
		<% if(so.hasRole("admin")){%>
			<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="editUser()">修改用户</a>
		<%}%>
		<% if(so.hasRole("admin")){%>
			<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="destroyUser()">删除用户</a>
		<%}%> 
	</div>


<div id="dlg" class="easyui-dialog" style="width:400px;height:120px;padding:10px 20px"
            closed="true" buttons="#dlg-buttons">
        <form id="fm" method="post" novalidate>
            <div class="fitem">
                <label>用户名称:</label>
                <input name="username" class="easyui-textbox" required="true" >
            </div>
        </form>
</div>
<div id="dlg-buttons">
	<a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveUser()" style="width:90px">保存</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">取消</a>
</div>

<div id="dlgUserRole-buttons">
	<a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveUserRole()" style="width:90px">保存</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlgUserRole').dialog('close')" style="width:90px">取消</a>
</div>
<div id="dlgUserRole" class="easyui-dialog" style="width:400px;height:160px;padding:10px 20px" 
	closed="true" buttons="#dlgUserRole-buttons">
        <form id="fmUserRole" method="post">
        	<div class="fitem">
                <label>用户名称:</label>
                <input name="username" class="easyui-textbox" style="width:200px;" required="true" >
            </div>
            <div class="fitem">
                <label>授权角色:</label>
                <select id="role_id" class="easyui-combotree" name="role_id" multiple style="width:200px;"
					data-options="valueField:'id',textField:'description',url:'/role/allRoles'" >
            </div>
        </form>
</div>

<script type="text/javascript">
        var url;
        function newUser(){
            $('#dlg').dialog('open').dialog('center').dialog('setTitle','新增用户');
            $('#fm').form('clear');
            url = '/user/add';
        }
        function editUser(){
            var row = $('#dg').datagrid('getSelected');
            if (row){
                $('#dlg').dialog('open').dialog('center').dialog('setTitle','修改用户');
                $('#fm').form('load',row);
                url = '/user/edit?id='+row.id;
            }
        }
        function saveUser(){
            $('#fm').form('submit',{
                url: url,
                onSubmit: function(){
                    return $(this).form('validate');
                },
                success: function(result){
                    var result = eval('('+result+')');
                    if (result.errorMsg){
                        $.messager.show({
                            title: 'Error',
                            msg: result.errorMsg
                        });
                    } else {
                        $('#dlg').dialog('close');        // close the dialog
                        $('#dg').datagrid('reload');    // reload the user data
                    }
                }
            });
        }
        function destroyUser(){
            var row = $('#dg').datagrid('getSelected');
            if (row){
                $.messager.confirm('Confirm','确认删除用户?',function(r){
                    if (r){
                        $.post('/user/remove',{id:row.id},function(result){
                            if (result.success){
                                $('#dg').datagrid('reload');    // reload the user data
                            } else {
                                $.messager.show({    // show error message
                                    title: 'Error',
                                    msg: result.errorMsg
                                });
                            }
                        },'json');
                    }
                });
            }
        }
    </script>
    
<script>
	var url;
	function formatOper(val,row,index){
		return '<a href="#" onclick="editUserRole('+index+')">修改角色</a>';
	}
	
	function editUserRole(index){
    	$('#dg').datagrid('selectRow',index);
    	var row = $('#dg').datagrid('getSelected');
    	if (row){
        	$('#dlgUserRole').dialog('open').dialog('setTitle','修改角色');  
        	$('#fmUserRole').form('load',row);  
        	url = '${cxt!}/user/manageRole?id='+row.id;
        	$.post('/user/getRoles',{id:row.id},function(data){
            	var arrayObj = new Array();
            	$.each(data, function(i, r) {
            		arrayObj.push(r.id);
            	});
            	$('#role_id').combotree('setValues',arrayObj);
			});  
    	}  
	}
	function saveUserRole(){
		$('#fmUserRole').form('submit',{
		url: url,
        onSubmit: function(){
			return $(this).form('validate');
		},
		success: function(result){
			var result = eval('('+result+')');
            if (result.errorMsg){
            	$.messager.show({
                    title: 'Error',
                    msg: result.errorMsg
                });
                } else {
                	$('#dlgUserRole').dialog('close');
                }
        }
      	});
	}
</script>    

<% include("../common/footer.html"){} %>